package com.hellpro.lolbox.home.hero;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.LolBaseActivity;
import com.hellpro.lolbox.bean.Hero_All;
import com.hellpro.lolbox.db.CollectDao;
import com.hellpro.lolbox.home.hero.herolist.Hero_list;
import com.yyydjk.library.DropDownMenu;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class Hero extends LolBaseActivity {


    @BindView(R.id.yx_search_view)
    SearchView yxSearchView;
    @BindView(R.id.dropDownMenuss)
    DropDownMenu dropDownMenu;
    @BindView(R.id.hero_zuihoudezhanyi)
    GridView heroGrid;
    @BindView(R.id.activity_hero_bar)
    Toolbar activityHeroBar;


    private String header[] = {"类型", "位置", "价格"};
    private List<View> popuViews;
    private String leiXing[] = {"全部", "新手", "战士", "法师", "刺客", "坦克", "辅助", "射手"};
    private String weiZhi[] = {"全部", "上单", "中单", "ADC", "打野", "辅助"};
    private String price[] = {"全部", "450", "1350", "3150", "4800", "6300"};
    private String LX = null;
    private String WZ = null;
    private String PC = null;


    private HeroGridAdapter heroGridAdapter;
    private SQLiteDatabase db;
    private List<Hero_All.AllBean> mListe;


    @Override
    public int getLayoutId() {
        return R.layout.activity_hero;
    }

    @Override
    public void initView() {
        activityHeroBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mListe = new ArrayList<>();


        popuViews = new ArrayList<>();
        //chaxun

        EventBus.getDefault().register(this);
        new DownHeroData(this);

        final View LeiXIngView = getLayoutInflater().inflate(R.layout.hero_item, null);
        final GridView leiXingGridView = ButterKnife.findById(LeiXIngView, R.id.hero_item);
        final MeanGridViewAdapter leiXingGridViewAdapter = new MeanGridViewAdapter(this, Arrays.asList(leiXing));
        leiXingGridView.setAdapter(leiXingGridViewAdapter);

        final View weiZhiView = getLayoutInflater().inflate(R.layout.hero_item, null);
        GridView weiZhiGridView = ButterKnife.findById(weiZhiView, R.id.hero_item);
        final MeanGridViewAdapter weiZhiGridViewAdapter = new MeanGridViewAdapter(this, Arrays.asList(weiZhi));
        weiZhiGridView.setAdapter(weiZhiGridViewAdapter);

        final View priceView = getLayoutInflater().inflate(R.layout.hero_item, null);
        GridView priceGridView = ButterKnife.findById(priceView, R.id.hero_item);
        final MeanGridViewAdapter priceGridViewAdapter = new MeanGridViewAdapter(this, Arrays.asList(price));
        priceGridView.setAdapter(priceGridViewAdapter);


        popuViews.add(LeiXIngView);
        popuViews.add(weiZhiView);
        popuViews.add(priceView);


        //设置监听
        leiXingGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<String> bb = new ArrayList<>();
                List<String> tbb = new ArrayList<>();
                leiXingGridViewAdapter.setCheckItem(position);
                String leiNAme = leiXing[position];
                dropDownMenu.setTabText(leiNAme);
                dropDownMenu.closeMenu();
                if (leiNAme.equals("战士")) {
                    leiNAme = "fighter";
                    LX = leiNAme;
                } else if (leiNAme.equals("法师")) {
                    leiNAme = "mage";
                    LX = leiNAme;
                } else if (leiNAme.equals("刺客")) {
                    leiNAme = "assassin";
                    LX = leiNAme;
                } else if (leiNAme.equals("坦克")) {
                    leiNAme = "tank";
                    LX = leiNAme;
                } else if (leiNAme.equals("辅助")) {
                    leiNAme = "support";
                    LX = leiNAme;
                } else if (leiNAme.equals("射手")) {
                    leiNAme = "marksman";
                    LX = leiNAme;
                } else {
                    leiNAme = "all";
                    LX = null;

                }
                ;
                if (leiNAme.equals("all") || leiNAme == null) {
                    Cursor hero_all = db.query("hero_all", null, null, null, null, null, null);
                    getSqliteData(hero_all);
                    return;
                }

                if (WZ == null) {
                    tbb.add("tags=?");
                    bb.add(LX);
                } else {
                    tbb.add("tags=?" + " " + "and" + " ");
                    bb.add(LX);
                }

                if (WZ != null) {
                    bb.add(WZ);
//                    tbb.add("location=?"+" "+"or"+" ");
//                    bb.add(WZ);
//                    tbb.add("location1=?"+" "+"or"+" ");
//                    bb.add(WZ);
//                   tbb.add("location2=?");
                    tbb.add("location=?");

                }


                if (PC != null) {
                    bb.add(PC);
                    tbb.add(" " + "and" + " " + "price=?");
                }


                String[] aaa = new String[bb.size()];
                for (int i = 0; i < bb.size(); i++) {
                    String bs = bb.get(i);
                    aaa[i] = bs;
                }
                //查询条件的
                String taaa = tbb.get(0);
                for (int i = 1; i < tbb.size(); i++) {
                    taaa += tbb.get(i);
                }


                Cursor hero_all = db.query("hero_all", null, taaa, aaa, null, null, null);
                getSqliteData(hero_all);
                hero_all.close();

            }
        });
        weiZhiGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                weiZhiGridViewAdapter.setCheckItem(position);
                String weiZhiNAme = weiZhi[position];
                dropDownMenu.setTabText(weiZhiNAme);
                dropDownMenu.closeMenu();
                List<String> cc = new ArrayList<>();
                List<String> tcc = new ArrayList<String>();
                if (weiZhiNAme.equals("全部") || weiZhiNAme == null) {
                    Cursor hero_all = db.query("hero_all", null, null, null, null, null, null);
                    getSqliteData(hero_all);
                    WZ = null;
                    return;
                }
                WZ = weiZhiNAme;
                if (LX != null) {
                    cc.add(LX);

                    tcc.add("tags=?" + " " + "and" + " ");


                }
                cc.add(WZ);
//                tcc.add("location=?"+" "+"or"+" ");
//                cc.add(WZ);
//                tcc.add("location1=?"+" "+"or"+" ");
//                cc.add(WZ);
//                tcc.add("location2=?");
                tcc.add("location=?");
                if (PC != null) {
                    cc.add(PC);
                    tcc.add(" " + "and" + " " + "price=?");
                }


                String[] bbb = new String[cc.size()];
                for (int i = 0; i < cc.size(); i++) {
                    String s = cc.get(i);
                    bbb[i] = s;
                }

                //查询条件的
                String tbbb = tcc.get(0);
                for (int i = 1; i < tcc.size(); i++) {
                    tbbb += tcc.get(i);
                }
                Cursor hero_all = db.query("hero_all", null, tbbb, bbb, null, null, null);
                getSqliteData(hero_all);
                hero_all.close();
            }
        });
        priceGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                priceGridViewAdapter.setCheckItem(position);
                String priceNAme = price[position];
                dropDownMenu.setTabText(priceNAme);
                dropDownMenu.closeMenu();
                List<String> tdd = new ArrayList<String>();

                List<String> dd = new ArrayList<>();
                if (priceNAme == null || priceNAme.equals("全部")) {
                    Cursor hero_all = db.query("hero_all", null, null, null, null, null, null);
                    getSqliteData(hero_all);
                    PC = null;
                    return;
                }
                PC = priceNAme;

                if (LX != null) {
                    dd.add(LX);
                    if (WZ == null) {
                        tdd.add("tags=?");
                    } else {
                        tdd.add("tags=?" + " " + "and" + " ");
                    }
                }

                if (WZ != null) {
                    dd.add(WZ);
//                    tdd.add("location=?"+" "+"or"+" ");
//                    dd.add(WZ);
//                    tdd.add("location1=?"+" "+"or"+" ");
//                    dd.add(WZ);
//                    tdd.add("location2=?");
                    tdd.add("location=?");

                }
                dd.add(PC);
                if (LX == null && WZ == null) {
                    tdd.add("price=?");
                } else {
                    tdd.add(" " + "and" + " " + "price=?");
                }

                String[] ccc = new String[dd.size()];
                for (int i = 0; i < dd.size(); i++) {
                    String s = dd.get(i);
                    ccc[i] = s;
                }

                //查询条件的
                String tccc = tdd.get(0);
                for (int i = 1; i < tdd.size(); i++) {
                    tccc += tdd.get(i);
                }

                Cursor hero_all = db.query("hero_all", null, tccc, ccc, null, null, null);
                getSqliteData(hero_all);
                hero_all.close();

            }
        });
        heroGridAdapter = new HeroGridAdapter(this);
        heroGrid.setAdapter(heroGridAdapter);

//必须有个 dropDownmean的弊端
        GridView gridView = new GridView(this);
        gridView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        gridView.setNumColumns(4);
        dropDownMenu.setDropDownMenu(Arrays.asList(header), popuViews, gridView);

        //搜索监听
        yxSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Cursor hell = db.query("hero_all", null, "cnName=?", new String[]{query}, null, null, null);
                getSqliteData(hell);
                hell.close();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        heroGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String enName = mListe.get(position).getEnName();
                Intent intent = new Intent(Hero.this, Hero_list.class);
                intent.putExtra("name", enName);
                startActivity(intent);

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(final Hero_All data) {
        final List<Hero_All.AllBean> all = data.getAll();
        new Thread(
                new Runnable() {
                    @Override
                    public void run() {

                        CollectDao collectDao = new CollectDao(Hero.this);
                        db = collectDao.getDb();
                        Cursor hero_all = db.query("hero_all", null, null, null, null, null, null);
                        int columnCount = hero_all.getCount();
                        if (columnCount == 0) {
                            //取出数据
                            for (int i = 0; i < all.size(); i++) {
                                Hero_All.AllBean allBean = all.get(i);
                                String cnName = allBean.getCnName();
                                String enName = allBean.getEnName();
                                String title = allBean.getTitle();
                                String location = allBean.getLocation();
                                String price = allBean.getPrice();
                                String[] prices = price.split(",");
                                String pri = prices[0];
                                String tags = allBean.getTags();
                                ContentValues contentValues = new ContentValues();
                                contentValues.put("enName", enName);
                                contentValues.put("cnName", cnName);
                                contentValues.put("title", title);
                                contentValues.put("tags", tags);
                                String[] split = location.split(",");
                                String lc = split[0];
                                contentValues.put("location", lc);
                                if (1 < split.length) {
                                    String lc1 = split[1];
                                    contentValues.put("location1", lc1);
                                    if (2 < split.length) {
                                        String lc2 = split[2];
                                        contentValues.put("location2", lc2);
                                    }
                                }
                                contentValues.put("price", pri);
                                db.insert("hero_all", null, contentValues);

                            }
                        }
                    }
                }
        ).start();
        heroGridAdapter.setData(all);
        mListe = all;

    }

    //遍历方法
    private void getSqliteData(Cursor cursor) {
        mListe.clear();
        while (cursor.moveToNext()) {
            int columnIndex = cursor.getColumnIndex("enName");
            String string = cursor.getString(columnIndex);
            int columnIndex1 = cursor.getColumnIndex("cnName");
            String string1 = cursor.getString(columnIndex1);
            Hero_All.AllBean allBean = new Hero_All.AllBean();
            allBean.setCnName(string1);
            allBean.setEnName(string);
            mListe.add(allBean);


        }
        if (mListe != null) {
            heroGridAdapter.setData(mListe);
        }

    }



}
